package org.flexthinker.binding.bean.support
{
    import org.flexthinker.binding.value.support.MockBean;

    import org.hamcrest.assertThat;
    import org.hamcrest.object.equalTo;
    import org.hamcrest.core.throws;
    import org.hamcrest.object.instanceOf;

    /**
     * @author Dragos Dascalita Haut, Nov 29, 2010
     */
    public class PropertyReaderTest
    {
        private var readObj:Object = {propInt:10, propString:"StringProp",
            propObj:{subProp1:"substr"}};


        public function PropertyReaderTest()
        {
        }

        [Test]
        public function testReadFromSingleProperty():void
        {
            var val:int = PropertyReader.getValue( readObj, "propInt" );
            assertThat( "int should have been read", val, equalTo( 10 ) );
            var strVal:String = PropertyReader.getValue( readObj, "propString" );
            assertThat( "string value should have been read", strVal, equalTo( "StringProp" ) );
        }

        [Test]
        public function testReadFromSubpaths():void
        {
            var strVal:String = PropertyReader.getValue( readObj, "propObj.subProp1" );
            assertThat( "string value should have been read", strVal, equalTo( "substr" ) );
        }

        [Test]
        public function testReadFromInexistingProperty():void
        {
            var noVal:* = PropertyReader.getValue( readObj, "inexistingPropOnDynamicObj" );
            assertThat( "noVal should have been null", noVal, equalTo( null ) );

            var str:String = new String( "xyz" );

            assertThat( function():void
            {
                PropertyReader.getValue( str, "inexistingPropOnTypedObject" )
            }, throws( instanceOf( PropertyResolutionError ) ) );
        }

        [Test]
        public function testReadFromSetterOnly():void
        {
            assertThat( function():void
            {
                var bean:MockBean = new MockBean();
                PropertyReader.getValue( bean, "setOnlyProp" );
            }, throws( instanceOf(PropertyResolutionError)) );
        }

        [Test]
        public function testReadFromGetterOnly():void
        {
            var bean:MockBean = new MockBean();
            var roVal:String = PropertyReader.getValue(bean,"getOnlyProp");
            assertThat(roVal, equalTo(bean.getOnlyProp));
        }
    }
}